package com.sharesrc.jdev.wiimotedemo.views;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog.ModalityType;
import java.awt.GridBagConstraints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import com.sharesrc.jdev.wiimotedemo.common.constant.Constants;
import com.sharesrc.jdev.wiimotedemo.common.constant.Themes;
import com.sharesrc.jdev.wiimotedemo.common.util.UIUtils;

public class DetailedInfoPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 201308161418L;

	public JLabel lblRawData, lblDataFiltedByIdleStateFilter, lblDataFiltedByMotionDetectFilter, lblDataFiltedByDirectionalEquivalenceFilter, lblDataFiltedByHighPassFilter, lblAbsoluteValue;
	public JTextField tfRawData, tfDataFiltedByIdleStateFilter, tfDataFiltedByMotionDetectFilter, tfDataFiltedByDirectionalEquivalenceFilter, tfDataFiltedByHighPassFilter, tfAbsoluteValue;

	DetailedInfo detailedInfo;

	EventsHandler handler;

	public DetailedInfo getDetailedInfo() {
		return this.detailedInfo;
	}

	public DetailedInfoPanel() {
		super();
		this.handler = new EventsHandler();
		initUIComponents();
	}

	public DetailedInfoPanel(DetailedInfo detailedInfo) {
		this();
		this.detailedInfo = detailedInfo;

		setRawData(this.detailedInfo.getRawData());
		setDataFiltedByIdleStateFilter(this.detailedInfo.getDataFiltedByIdleStateFilter());
		setDataFiltedByMotionDetectFilter(this.detailedInfo.getDataFiltedByMotionDetectFilter());
		setDataFiltedByDirectionalEquivalenceFilter(this.detailedInfo.getDataFiltedByDirectionalEquivalenceFilter());
		setDataFiltedByHighPassFilter(this.detailedInfo.getDataFiltedByHighPassFilter());
		setAbsoluteValue(this.detailedInfo.getAbsoluteValue());
	}

	private void initUIComponents() {
		this.lblRawData = new JLabel("▼ " + Constants.Texts.RAW_DATA);
		this.tfRawData = new JTextField();
		this.lblDataFiltedByIdleStateFilter = new JLabel("    2-" + Constants.Texts.DATA_FILTED_BY_IDLESTATEFILTER);
		this.tfDataFiltedByIdleStateFilter = new JTextField();
		this.lblDataFiltedByMotionDetectFilter = new JLabel("    3-" + Constants.Texts.DATA_FILTED_BY_MOTIONDETECTFILTER);
		this.tfDataFiltedByMotionDetectFilter = new JTextField();
		this.lblDataFiltedByDirectionalEquivalenceFilter = new JLabel("    4-" + Constants.Texts.DATA_FILTED_BY_DIRECTIONALEQUIVALENCEFILTER);
		this.tfDataFiltedByDirectionalEquivalenceFilter = new JTextField();
		this.lblDataFiltedByHighPassFilter = new JLabel("    5-" + Constants.Texts.DATA_FILTED_BY_HIGHPASSFILTER);
		this.tfDataFiltedByHighPassFilter = new JTextField();
		this.lblAbsoluteValue = new JLabel("    " + Constants.Texts.ABSOLUTE_VALUE);
		this.tfAbsoluteValue = new JTextField();

		addLine(this.lblRawData, this.tfRawData);
		addLine(this.lblAbsoluteValue, this.tfAbsoluteValue);

		setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Themes.Colore.DETAILED_INFO_PANEL_BORDER));

		addMouseListener(this.handler);
	}

	private void addLine(JLabel lbl, JTextField tf) {
		addLine(this, lbl, tf);
	}

	private void addLine(Container container, JLabel lbl, JTextField tf) {
		lbl.setOpaque(true);
		lbl.setBackground(Themes.Colore.DETAILED_INFO_PANEL_HEADLINE_BG);
		lbl.setForeground(Themes.Colore.DETAILED_INFO_PANEL_HEADLINE_FG);
		lbl.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Themes.Colore.DETAILED_INFO_PANEL_BORDER), BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		tf.setOpaque(true);
		tf.setEditable(false);
		tf.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Themes.Colore.DETAILED_INFO_PANEL_BORDER), BorderFactory.createEmptyBorder(5, 15, 5, 5)));

		UIUtils.addComponent(container, lbl, 1, 1, 1, 1, GridBagConstraints.BOTH, 0, 0, 0, 0, GridBagConstraints.LINE_START);
		UIUtils.addComponent(container, tf, GridBagConstraints.REMAINDER, 1, 5, 1, GridBagConstraints.BOTH, 0, 0, 0, 0, GridBagConstraints.CENTER);
	}

	public void setRawData(String value) {
		this.tfRawData.setText(value);
	}

	public void setDataFiltedByIdleStateFilter(String value) {
		this.tfDataFiltedByIdleStateFilter.setText(value);
	}

	public void setDataFiltedByMotionDetectFilter(String value) {
		this.tfDataFiltedByMotionDetectFilter.setText(value);
	}

	public void setDataFiltedByDirectionalEquivalenceFilter(String value) {
		this.tfDataFiltedByDirectionalEquivalenceFilter.setText(value);
	}

	public void setDataFiltedByHighPassFilter(String value) {
		this.tfDataFiltedByHighPassFilter.setText(value);
	}

	public void setAbsoluteValue(String value) {
		this.tfAbsoluteValue.setText(value);
	}

	class EventsHandler implements MouseListener {

		@Override
		public void mouseClicked(MouseEvent e) {

		}

		@Override
		public void mousePressed(MouseEvent e) {

		}

		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.getClickCount() == 2) {
				e.consume();
				JDialog dialog = new JDialog();
				DetailedInfoPanel detailedInfoPanel = new DetailedInfoPanel(DetailedInfoPanel.this.getDetailedInfo());
				detailedInfoPanel.lblRawData.setText("▼ 1-" + Constants.Texts.RAW_DATA);

				addLine(detailedInfoPanel, detailedInfoPanel.lblRawData, detailedInfoPanel.tfRawData);
				addLine(detailedInfoPanel, detailedInfoPanel.lblDataFiltedByIdleStateFilter, detailedInfoPanel.tfDataFiltedByIdleStateFilter);
				addLine(detailedInfoPanel, detailedInfoPanel.lblDataFiltedByMotionDetectFilter, detailedInfoPanel.tfDataFiltedByMotionDetectFilter);
				addLine(detailedInfoPanel, detailedInfoPanel.lblDataFiltedByDirectionalEquivalenceFilter, detailedInfoPanel.tfDataFiltedByDirectionalEquivalenceFilter);
				addLine(detailedInfoPanel, detailedInfoPanel.lblDataFiltedByHighPassFilter, detailedInfoPanel.tfDataFiltedByHighPassFilter);
				addLine(detailedInfoPanel, detailedInfoPanel.lblAbsoluteValue, detailedInfoPanel.tfAbsoluteValue);

				dialog.getContentPane().add(new JScrollPane(detailedInfoPanel, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
				dialog.setModal(true);
				dialog.setModalityType(ModalityType.APPLICATION_MODAL);
				dialog.setTitle(Constants.Messages.DETAILED_INFO_DIALOG_TITLE);
				dialog.setSize(800, 400);
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {

		}

		@Override
		public void mouseExited(MouseEvent e) {

		}

	}
}
